<template>
    <el-dialog
        title="指定APK版本"
        :visible.sync="showValue"
        append-to-body
        :close-on-click-modal="false"
        width="1100px"
        top="30px"
        :close="clearData"
        :before-close="clearData"
    >
        <div>
            <el-form ref="form" size="small" inline :model="form">
                <el-form-item label="设备类型:" prop="terminalType">
                    <el-select
                        class="items"
                        v-model="form.terminalType"
                        filterable
                        @change="getZDList"
                        placeholder="请选择"
                        size="small"
                    >
                        <el-option
                            v-for="(item, index) in terminalTypeList"
                            :key="index"
                            :label="item.terminalTypeName"
                            :value="item.terminalType"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item
                    label="升级包文件类型："
                    prop="upgradePackageFileType"
                >
                    <el-select
                        class="items"
                        v-model="form.upgradePackageFileType"
                        filterable
                        @change="getZDList"
                        placeholder="请选择"
                        size="small"
                    >
                        <el-option
                            v-for="(item, index) in packageFileTypeList"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item
                    v-if="zdList.length > 0"
                    label="终端固件包类型："
                    prop="firmwareTerminalType"
                >
                    <el-select
                        class="items"
                        v-model="form.firmwareTerminalType"
                        filterable
                        @change="search"
                        placeholder="请选择"
                        size="small"
                    >
                        <el-option
                            v-for="(item, index) in zdList"
                            :key="index"
                            :label="item.terminalTypeName"
                            :value="item.terminalType"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-button type="primary" size="small" @click="search"
                    >查询</el-button
                >
            </el-form>
            <el-table
                v-loading="loading"
                :header-cell-style="headerCellStyle"
                :data="tableData"
                size="small"
                max-height="350px"
            >
                <el-table-column
                    prop="packageName"
                    label="升级包名称"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="packageVersionCode"
                    label="升级包版本号"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="internalPackageVersionCode"
                    label="内部版本号"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="upgradePackageFileTypeName"
                    label="升级包文件类型"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="terminalTypeName"
                    label="设备类型"
                    show-overflow-tooltip
                ></el-table-column>

                <el-table-column
                    prop="createTime"
                    label="创建时间"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column fixed="right" align="center" label="操作">
                    <template slot-scope="scope">
                        <el-button
                            size="small"
                            type="primary"
                            @click="checkApk(scope.row)"
                            >指定版本</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </div>
    </el-dialog>
</template>

<script>
export default {
    props: {
        value: Boolean,
        currRow: Object
    },
    data () {
        return {
            zdList: [],
            currentPage: 1,
            pageSize: 10,
            totalNum: 0,
            loading: false,
            tableData: [],
            showValue: false,
            terminalTypeList: [],
            packageFileTypeList: [],
            form: {
                terminalType: '',
                upgradePackageFileType: ''
            }
        }
    },
    watch: {
        value (val) {
            this.showValue = val
        },
        showValue (val) {
            this.$emit('update:value', val)
            if (val) {
                this.getList()
            }
        }
    },
    created () {
        this.getTerminalTypeByType()
        this.getUpgradePackageFileType()
    },
    methods: {
        async getZDList () {
            let terminalTypeValue = null
            this.terminalTypeList.forEach(item => {
                if (item.terminalType === this.form.terminalType) {
                    terminalTypeValue = item.terminalTypeValue
                }
            })
            console.log(this.terminalTypeList)
            this.$http.post(this.$store.state.IP + '/system/company/getFirmwareTerminalType', {
                firmwarePackageType: this.form.upgradePackageFileType,
                terminalTypeValue: terminalTypeValue
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    this.zdList = response.res
                    if (this.zdList.length > 0) {
                        this.form.firmwareTerminalType = this.zdList[0].terminalType
                    }
                    this.search()
                }
            }, response => {
            })
        },
        search () {
            this.currentPage = 1
            this.getList()
        },
        getUpgradePackageFileType () {
            this.$http.get(this.$store.state.IP + '/upgradeManage/getUpgradePackageFileType', {
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    this.packageFileTypeList = response.res
                    this.form.upgradePackageFileType = 1
                }
            }, response => {
            })
        },
        getTerminalTypeByType () {
            this.$http.get(this.$store.state.IP + '/system/company/getOnlyTerminalTypeV2?isAll=1',
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    this.terminalTypeList = response.res
                    this.form.terminalType = this.terminalTypeList[0].terminalType
                }
            }, response => {
            })
        },
        checkApk (row) {
            this.$confirm('确定执行此操作吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const res = await this.$http.post(this.$store.state.IP + '/upgradeManage/addUpgradeTask', {
                    listUpgradeTaskBaseInfo: [this.currRow],
                    upgradePackageID: row.upgradePackageID,
                    upgradeStage: 2
                })
                let response = res.data
                if (response.ret === '0') {
                    this.$message({
                        message: '指定成功',
                        type: 'success'
                    })
                    this.getList()
                    this.$emit('getPage')
                }
            }).catch(() => {
            })
        },
        handleCurrentChange (page) {
            // 换页
            this.currentPage = page
            this.getList()
        },
        // 获取列表
        async getList () {
            this.loading = true
            const res = await this.$http.post(this.$store.state.IP + '/upgradeManage/agentCanUseUpgradePackageList', {
                companyCode: this.currRow.companyCode,
                ...this.form
            })
            let response = res.data
            if (response.ret === '0') {
                this.loading = false
                this.tableData = response.res
            } else {
                this.loading = false
            }
        },
        // 清空数据
        clearData () {
            this.$refs['form'].resetFields()
            this.zdList = []
            this.currentPage = 1
            this.showValue = false
            this.tableData = []
        }
    }
}
</script>
<style lang="stylus" rel="stylesheet/stylus" scoped>
.item
    width: 110px

.right
    text-align: right
</style>